#!/usr/bin/env python
# coding=utf-8
# CopyRight(c) 2019 Inspur Electronic Information Industry Co.,Ltd

"""
    secchecksuite/python_version_factory.py
    针对不同的python版本执行不同的代码
    :CopyRight(c) 2019 Inspur Electronic Information Industry Co.,Ltd
"""
import io
import sys
import imp
import json


class PythonVersion:
    """
    Base python version class
    Attributes:
        None
    """

    def __init__(self):
        pass

    def input_info(self, str_input):
        pass

    def set_default_encoding(self):
        pass

    def save_config_file(self, path, data):
        pass

    def str(self, bytes):
        pass

    def decode(self, data):
        pass


class PythonVersion2(PythonVersion):
    """
    python2.x version class
    Attributes:
        None
    """

    def __init__(self):
        PythonVersion.__init__(self)

    def input_info(self, str_input):
        """
        input something on console
        Args:
            str_input: input content
        Returns:
            None
        """
        return raw_input(str_input)

    def set_default_encoding(self):
        """
        setdefaultencoding 'utf-8' for python2.x
        Returns:
            None
        """
        imp.reload(sys)
        sys.setdefaultencoding('utf-8')

    def save_config_file(self, path, data):
        try:
            with io.open(path, 'w', encoding='utf-8') as f:
                json_data = json.dumps(data, f, indent=4, ensure_ascii=False)
                f.write(json_data)
            f.close()
            print("Save Success.")
            return True
        except IOError as err:
            print("Save Fail. Occur some errors: {}.".format(err))
            return False

    def str(self, bytes):
        return str(bytes)

    def decode(self, data):
        if isinstance(data, str):
            return data.decode('utf-8')
        if isinstance(data, list):
            result = []
            for item in data:
                if isinstance(item, str):
                    result.append(item.decode('utf-8'))
                else:
                    result.append(item)
            return '[' + ','.join(result) + ']'
        return str(data)


class PythonVersion3(PythonVersion):
    """
    python3.x version class
    Attributes:
        None
    """

    def __init__(self):
        PythonVersion.__init__(self)

    def input_info(self, str_input):
        """
        input something on console
        Args:
            str_input: input content
        Returns:
            None
        """
        return input(str_input)

    def set_default_encoding(self):
        """
        python3.x do nothing
        Returns:
            None
        """
        pass

    def save_config_file(self, path, data):
        try:
            with open(path, 'w', encoding='utf-8') as f:
                json.dump(data, f, indent=4, ensure_ascii=False)
            f.close()
            print("Save Success.")
            return True
        except IOError as err:
            print("Save Fail. Occur some errors: {}.".format(err))
            return False

    def str(self, bytes):
        return str(bytes, encoding='utf-8')

    def decode(self, data):
        if isinstance(data, list):
            return '[' + ','.join(data) + ']'
        return str(data)
